package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.query.PaymentScheduleDetailQueryObject;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleDetailService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 我要借款
 */
@Controller
public class BorrowController {

    @Autowired
    private IAccountService accountService;
    @Autowired
    private IUserInfoService infoService;
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IPaymentScheduleService paymentScheduleService;

    /**
     * 显示我要借款页面
     *
     * @param model
     * @return
     */
    @RequestMapping("borrow")
    public String borrow(Model model) {
        //如果有登陆跳转到视图页面,
        LoginInfo loginInfo = UserContext.getLoginInfo();
        if (loginInfo == null) {
            return "redirect:/borrow.html";
        }
        //账户信息
        Account account = accountService.getById(loginInfo.getId());
        model.addAttribute("account", account);
        //用户信息
        UserInfo userinfo = infoService.getById(loginInfo.getId());
        model.addAttribute("userinfo", userinfo);
        return "borrow";
    }

    /**
     * 申请贷款
     *
     * @return
     */
    @RequestMapping("borrowInfo")
    public String borrowInfo(Model model) {
        UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());
        if (userInfo.hasBidRequestInProcess()) {
            return "borrow_apply_result";
        }

        //最小借款金额
        model.addAttribute("minBidRequestAmount", Constants.BORROW_MIN_AMOUNT);

        //用户账户
        model.addAttribute("account", accountService.getById(UserContext.getLoginInfo().getId()));

        //最小投标金额
        model.addAttribute("minBidAmount", Constants.BID_MIN_AMOUNT);

        //最小借款利率
        model.addAttribute("minCurrentRate", Constants.MIN_CURRENTRATE);

        //最大借款利率
        model.addAttribute("maxCurrentRate", Constants.MAX_CURRENTRATE);

        return "borrow_apply";
    }

    /**
     * 申请
     *
     * @param bidRequest
     * @return
     */
    @RequestMapping("borrow_apply")
    @ResponseBody
    public JsonResult borrowApply(BidRequest bidRequest) {
        bidRequestService.apply(bidRequest);
        return JsonResult.instance();
    }

    /**
     * 前台还款明细
     *
     * @return
     */
    @RequestMapping("borrowBidReturn_list")
    public String borrowBidReturnList(Model model, @ModelAttribute("qo") PaymentScheduleDetailQueryObject qo) {

        qo.setBorrowUserId(UserContext.getLoginInfo().getId());
        model.addAttribute("pageInfo", paymentScheduleService.listForPage(qo));
        model.addAttribute("account", accountService.getById(UserContext.getLoginInfo().getId()));

        return "returnmoney_list";
    }

    /**
     * 立刻还款
     *
     * @return
     */
    @RequestMapping("returnMoney")
    @ResponseBody
    public JsonResult returnMoney(Long id) {

        bidRequestService.returnMoney(id);
        return JsonResult.instance();
    }


}
